<?php

namespace app\admin\controller;

use app\admin\model\Article;
use app\admin\model\Category as ModelCategory;
use app\admin\model\CategoryAdmin as ModelCategoryAdmin;
use app\admin\model\User;
use app\api\model\Category;
use app\api\model\User as ModelUser;
use app\common\LogService;
use think\facade\Db;

class CategoryAdmin  extends Base
{
    public function index()
    {
        return view();
    }
    public function list()
    {
        $user_id = input('user_id');
        $cate_id = input('cate_id');
        $status = input('status');
        $query = ['user_id' => $user_id, 'cate_id' => $cate_id, 'status' => $status];
        $list = ModelCategoryAdmin::list($query);

        return success($list);
    }
    public function add()
    {
        $user = ModelUser::field('id,nickname,qq')->select();
        $cate = Category::field('id,cate_name')->where('pid', 0)->select();
        return view('add', ['user' => $user, 'cate' => $cate]);
    }
    public function doAdd()
    {
        $recordLog = function ($status, $message = '') {
            LogService::record(
                name: '添加版主',
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: 2

            );
        };
        $user_id = input('user_id');
        $cate_id = input('cate_id');
        if ($user_id == '') {
            $recordLog(0, '用户ID不能为空');
            return error('用户ID不能为空');
        }
        if ($cate_id == '') {
            $recordLog(0, '分类ID不能为空');
            return error('分类ID不能为空');
        }
        $has = Db::name('category_admin')->where(['user_id' => $user_id, 'cate_id' => $cate_id])->find();
        if ($has) {
            $recordLog(0, '已存在该版主');
            return error('已存在该版主');
        }
        $data = ['user_id' => $user_id, 'cate_id' => $cate_id, 'status' => 1];
        Db::startTrans();
        try {
            $res = ModelCategoryAdmin::create($data);
            User::where('id', $user_id)->update(['role' => 2]);
            if ($res) {
                $recordLog(1);
                return success();
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
       
        $recordLog(0);
        return error();
    }
    public function edit($id)
    {
        $data = ModelCategoryAdmin::with(['userInfo' => function ($query) {
            $query->field('id,nickname');
        }, 'cateInfo' => function ($query) {
            $query->field('id,cate_name');
        }])->where('id', $id)->find()->toArray();
        $user = ModelUser::field('id,nickname,qq')->select();
        $cate = Category::field('id,cate_name')->where('pid', 0)->select();
        return view('edit', ['data' => $data, 'user' => $user, 'cate' => $cate]);
    }
    public function doEdit($id)
    {
        $recordLog = function ($status, $message = '') {
            LogService::record(
                name: '修改版主',
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: 2

            );
        };
        $user_id = input('user_id');
        $cate_id = input('cate_id');
        $status = input('status');
        if ($user_id == '') {
            $recordLog(0, '用户ID不能为空');
            return error('用户ID不能为空');
        }
        if ($cate_id == '') {
            $recordLog(0, '分类ID不能为空');
            return error('分类ID不能为空');
        }
        $has = Db::name('category_admin')->where(['user_id' => $user_id, 'cate_id' => $cate_id,'status' => $status])->find();
        if ($has) {
            $recordLog(0, '已存在该版主');
            return error('已存在该版主');
        }
        $data = ['user_id' => $user_id, 'cate_id' => $cate_id, 'status' => $status];
        Db::startTrans();
        try {
            $res = ModelCategoryAdmin::where('id',$id)->update($data);
            $role = $status == 0 ? 1 : 2;
            User::where('id', $user_id)->update(['role' => $role]);
            // 提交事务
            Db::commit();
            if ($res) {
                $recordLog(1);
                return success();
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        $recordLog(0);
        return error();
    }

    public function del($id)
    {
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '删除版主',
                target_id: $id,
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: 2

            );
        };
        $cate = ModelCategoryAdmin::where(['id' => $id])->find();
        if (!$cate) {
            $recordLog(0, '版主不存在');
            return error('版主不存在');
        }
        $res = ModelCategoryAdmin::destroy($id);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
}
